﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.Messaging;

namespace TaskTrackerMVC.Controllers
{
    public class AccountController : Controller
    {
        const string GOOGLE_PROVIDER = "https://www.google.com/accounts/o8/id";
        const string AUTH_COOKIE = "timetracker.gsquaredsoftware.com";

        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
        }

        // **************************************
        // URL: /Account/LogOn
        // **************************************

        public ActionResult LogOn()
        {
            var openid = new OpenIdRelyingParty();    
            IAuthenticationResponse response = openid.GetResponse();  
   
            if (response != null)    
            {
        
                switch (response.Status)        
                {            
                    case AuthenticationStatus.Authenticated:
                        // Remove any authentication cookies
                        if (Response.Cookies[AUTH_COOKIE] != null)
                        {
                            Response.Cookies.Remove(AUTH_COOKIE);
                        }

                        // Add the authentication cookie
                        var authCookie = new HttpCookie(AUTH_COOKIE, response.GetExtension<ClaimsResponse>().Email);
                        Response.Cookies.Add(authCookie);

                        FormsAuthentication.RedirectFromLoginPage(response.GetExtension<ClaimsResponse>().Email, false);                
                        break;            
                    case AuthenticationStatus.Canceled:               
                        ModelState.AddModelError("loginIdentifier","Login was cancelled at the provider");                
                        break;            
                    case AuthenticationStatus.Failed:                
                        ModelState.AddModelError("loginIdentifier", "Login failed using the provided OpenID identifier");
                        break;        
                }    
            }
            return View();
        } 
        
        [HttpPost]
        public ActionResult LogOn(string loginIdentifier)
        {    
            if (!Identifier.IsValid(loginIdentifier))    
            {        
                ModelState.AddModelError("loginIdentifier", "The specified login identifier is invalid");        
                return View();    
            }    
            else    
            {        
                var openid = new OpenIdRelyingParty();        
                IAuthenticationRequest request = openid.CreateRequest(Identifier.Parse(loginIdentifier));         // Require some additional data        
                request.AddExtension(new ClaimsRequest        
                                        {            
                                            BirthDate = DemandLevel.NoRequest,            
                                            Email = DemandLevel.Require,            
                                            FullName = DemandLevel.Require        
                                        });
 


                // Redirect to the Time Entry page
                return request.RedirectingResponse.AsActionResult();
            }
        }
    }
}
